package com.enki.adminplatform.security;

import cn.hutool.json.JSONUtil;
import com.enki.adminplatform.common.Result;
import com.enki.adminplatform.common.ResultCodeEnum;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author yinyin
 * @create 2021/07/27 下午 4:25
 */
@Component
public class LoginSuccessHandler implements AuthenticationSuccessHandler {
    //处理成功请求
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        response.setContentType("application/json;charest=UTF-8");
        ServletOutputStream outputStream= response.getOutputStream();
        //生成jwt并放入响应头

        Result result= Result.setResult(ResultCodeEnum.LOGIN_SUCCESS);
        outputStream.write(JSONUtil.toJsonStr(result).getBytes("UTF-8"));

        outputStream.flush();
        outputStream.close();
    }
}
